﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Configuration;
using System.Data;

namespace HY.WEB
{
    public partial class _default : PageBase
    {

        public string title = ConfigurationManager.AppSettings["title"];
        public string content = ConfigurationManager.AppSettings["content"];
        public string hico = ConfigurationManager.AppSettings["hico"];
        public string Description = ConfigurationManager.AppSettings["Description"];
        public string Keywords = ConfigurationManager.AppSettings["Keywords"];
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                // 设置样式和语言下拉列表的选中值
                ddlTheme.SelectedValue = PageManager1.Theme.ToString().ToLower();
                ddlLanguage.SelectedValue = PageManager1.Language.ToString().ToLower();

                // 显示源代码按钮
            //    btnSourceCode.OnClientClick = windowSourceCode.GetShowReference("./source.aspx?files=default.aspx;menu.xml;Web.config;Code/PageBase.cs;js/default.js");

              //  setmenu(); //设置菜单
                // 绑定 XML 数据源到树控件
              // treeMenu.DataSource = XmlDataSource1;
              // treeMenu.DataBind();
               fillmenu();
                btuser.Text = Session["user"].ToString();
               
            }
        }

        public void setmenu()
        {
            string bh = Session["bh"].ToString().Trim();
            DataTable dt = BLL.Bllbase.get_table("user_safe","ubh='"+bh+"'");
            if (dt.Rows.Count > 0)
            {
                XmlDataSource1.DataFile = "~/" + dt.Rows[0]["menu"].ToString().Trim();
            }
            else
            {
                ExtAspNet.Alert.ShowInParent("请确认账号已分配权限,或与管理员联系!", "提示", ExtAspNet.MessageBoxIcon.Information, "top.location.href='default.aspx'");
            }


        }

        protected void frmmain_exit(object sender, EventArgs e)
        {
           Session.Abandon();//取消当前会话   
           Session.Clear();//清除所有session   
            this.Response.Redirect("defaultsys.aspx");  
        }



        /// <summary>
        /// 修改样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpCookie themeCookie = new HttpCookie("Theme", ddlTheme.SelectedValue);
            themeCookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(themeCookie);
            PageContext.Refresh();
        }

        /// <summary>
        /// 修改语言
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpCookie langCookie = new HttpCookie("Language", ddlLanguage.SelectedValue);
            langCookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(langCookie);

            PageContext.Refresh();
        }

        void fillmenu()
        {
            string bh = Session["bh"].ToString().Trim();
            string menuSID = Session["menuSID"].ToString(); //菜单ID          
           // treeMenu.EnableSingleExpand = true;

            Boolean showhide;
          //DataTable dt = BLL.Bllbase.get_table("sys_menu",""," order by isorder asc");
            DataTable dt = BLL.Bllbase.Get_Menu_safe_Read(menuSID);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            //指定数据集中字段的父子关系
            ds.Relations.Add("baohan", ds.Tables[0].Columns["sid"], ds.Tables[0].Columns["owenid"],false);
            //循环表格行找到父节点行
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                showhide = Convert.ToBoolean(dr["showstatus"]);//是否有效显示
               
                if (dr["owenid"].ToString()=="0"&showhide) 
                {
                    ExtAspNet.TreeNode tn = new ExtAspNet.TreeNode();
                    tn.SingleClickExpand = true;
                    tn.Text = dr["m_name"].ToString();
                    //tn.Expanded = true;
                    tn.Leaf = Convert.ToBoolean(dr["lev"]);
                    treeMenu.Nodes.Add(tn);
                    //循环得到父节点的子节点
                    ResolveSubTree(dr, tn);
                }

            }

                 
        }

        private void ResolveSubTree(DataRow dataRow, ExtAspNet.TreeNode treeNode)
        {
            Boolean showhide;
            //循环其父节点的子节点的行
            foreach (DataRow row in dataRow.GetChildRows("baohan"))
            {
                showhide = Convert.ToBoolean(row["showstatus"]); //是否有效显示
                if (showhide)
                {
                    ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
                    node.Text = row["m_name"].ToString();
                    node.Leaf = Convert.ToBoolean(row["lev"]);
                    node.NavigateUrl = row["m_link"].ToString();
                    //  node.EnablePostBack = true;
                    treeNode.Nodes.Add(node);
                    //继续循环子节点看子节点是否还有子节点
                    ResolveSubTree(row, node);
                }
            }

        }


        protected void Tree1_NodeCommand(object sender, ExtAspNet.TreeCommandEventArgs e)
        {
            ExtAspNet.TreeNode node = treeMenu.FindNode(e.NodeID);
           // ExtAspNet.Alert.Show((node.Checked).ToString());
        }
        protected void Tree1_NodeCheck(object sender, ExtAspNet.TreeCheckEventArgs e)
        {
            ExtAspNet.TreeNode node = treeMenu.FindNode(e.NodeID);
            //ExtAspNet.Alert.Show((node.Checked).ToString() + "Tree1_NodeCheck");
        }

        /*
        private DataTable creatDataTable()
        {
            DataTable dt = new DataTable();
            DataColumn[] dc1 = new DataColumn[4];
            dc1[0] = new DataColumn("ID", typeof(int));
            dc1[1] = new DataColumn("Text", typeof(string));
            dc1[2] = new DataColumn("Leaf", typeof(Boolean));
            dc1[3] = new DataColumn("parentID", typeof(int));
            dt.Columns.AddRange(dc1);
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "中国";
            dr[2] = false;
            dr[3] = DBNull.Value;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 2;
            dr[1] = "湖南";
            dr[2] = false;
            dr[3] = 1;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 3;
            dr[1] = "常德";
            dr[2] = true;
            dr[3] = 2;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 4;
            dr[1] = "衡阳";
            dr[2] = true;
            dr[3] = 2;
            dt.Rows.Add(dr);
            return dt;
        }
        */
    }
}
